package org.lanyu.springainovel.common.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 任务通知实体类 - 合并了异步任务和通知功能
 * 用于统一管理异步任务和通知信息，简化系统复杂度
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("task_notification")
public class TaskNotification {

    /**
     * 主键ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 任务ID - 用于标识特定任务
     */
    @TableField("task_id")
    private String taskId;

    /**
     * 用户ID
     */
    @TableField("user_id")
    private Long userId;

    /**
     * 任务类型 - 如NOVEL_OUTLINE_GENERATION、CHAPTER_CONTENT_GENERATION等
     */
    @TableField("task_type")
    private String taskType;

    /**
     * 任务状态 - PENDING、RUNNING、COMPLETED、FAILED、CANCELLED
     */
    @TableField("status")
    private String status;

    /**
     * 任务标题 - 用于通知显示
     */
    @TableField("title")
    private String title;

    /**
     * 任务/通知内容
     */
    @TableField("content")
    private String content;

    /**
     * 任务结果 - JSON格式存储
     */
    @TableField("result")
    private String result;

    /**
     * 错误信息
     */
    @TableField("error_message")
    private String errorMessage;

    /**
     * 进度百分比 - 0-100
     */
    @TableField("progress")
    private Integer progress;

    /**
     * 是否已读 - false表示未读，true表示已读
     */
    @TableField("is_read")
    private Boolean isRead;

    /**
     * 是否为任务 - true表示是任务，false表示是普通通知
     */
    @TableField("is_task")
    private Boolean isTask;

    /**
     * 优先级 - LOW、MEDIUM、HIGH
     */
    @TableField("priority")
    private String priority;

    /**
     * 创建时间
     */
    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    /**
     * 完成时间
     */
    @TableField("completed_at")
    private LocalDateTime completedAt;

    /**
     * 是否已删除 - 逻辑删除标记
     */
    @TableField("is_deleted")
    @TableLogic
    private Boolean isDeleted;

    /**
     * 构造函数 - 用于创建任务
     */
    public TaskNotification(String taskId, Long userId, String taskType, String title, String content) {
        this.taskId = taskId;
        this.userId = userId;
        this.taskType = taskType;
        this.title = title;
        this.content = content;
        this.status = "PENDING";
        this.progress = 0;
        this.isRead = false;
        this.isTask = true;
        this.priority = "MEDIUM";
        this.isDeleted = false;
    }

    /**
     * 构造函数 - 用于创建通知
     */
    public TaskNotification(Long userId, String title, String content, String priority) {
        this.userId = userId;
        this.title = title;
        this.content = content;
        this.status = "COMPLETED";
        this.progress = 100;
        this.isRead = false;
        this.isTask = false;
        this.priority = priority != null ? priority : "MEDIUM";
        this.isDeleted = false;
    }

    /**
     * 默认构造函数
     */
    public TaskNotification() {
        this.isRead = false;
        this.isTask = true;
        this.priority = "MEDIUM";
        this.isDeleted = false;
    }

    /**
     * 标记为已读
     */
    public void markAsRead() {
        this.isRead = true;
    }

    /**
     * 标记为未读
     */
    public void markAsUnread() {
        this.isRead = false;
    }

    /**
     * 更新任务进度
     */
    public void updateProgress(int progress, String statusMessage) {
        this.progress = Math.max(0, Math.min(100, progress));
        if (statusMessage != null) {
            this.content = statusMessage;
        }
        
        // 根据进度更新状态
        if (progress <= 0) {
            this.status = "PENDING";
        } else if (progress < 100) {
            this.status = "RUNNING";
        } else {
            this.status = "COMPLETED";
            this.completedAt = LocalDateTime.now();
        }
    }

    /**
     * 标记任务为失败
     */
    public void markAsFailed(String errorMessage) {
        this.status = "FAILED";
        this.errorMessage = errorMessage;
        this.completedAt = LocalDateTime.now();
    }

    /**
     * 标记任务为取消
     */
    public void markAsCancelled() {
        this.status = "CANCELLED";
        this.completedAt = LocalDateTime.now();
    }

    /**
     * 完成任务
     */
    public void completeTask(String result) {
        this.status = "COMPLETED";
        this.progress = 100;
        this.result = result;
        this.completedAt = LocalDateTime.now();
    }

    /**
     * 检查是否为活动任务
     */
    public boolean isActiveTask() {
        return isTask && ("PENDING".equals(status) || "RUNNING".equals(status));
    }

    /**
     * 检查是否为已完成任务
     */
    public boolean isCompletedTask() {
        return isTask && ("COMPLETED".equals(status) || "FAILED".equals(status) || "CANCELLED".equals(status));
    }

    /**
     * 获取状态显示文本
     */
    public String getStatusDisplay() {
        switch (status) {
            case "PENDING": return "等待中";
            case "RUNNING": return "进行中";
            case "COMPLETED": return "已完成";
            case "FAILED": return "失败";
            case "CANCELLED": return "已取消";
            default: return status;
        }
    }

    /**
     * 获取任务类型显示文本
     */
    public String getTaskTypeDisplay() {
        if (!isTask) return "通知";
        
        switch (taskType) {
            case "NOVEL_OUTLINE_GENERATION": return "大纲生成";
            case "CHAPTER_OUTLINE_GENERATION": return "章节细纲生成";
            case "CHAPTER_CONTENT_GENERATION": return "章节内容生成";
            default: return taskType;
        }
    }
}